package com.zwface.demo.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.zwface.demo.R;


/**
 * 按钮倒计时工具
 */
public class CountDownTimerUtils extends CountDownTimer
{
    private TextView mTextView;
    private Context context;

    /**
     * @param millisInFuture  The number of millis in the future from the call
     *             to {@link #start()} until the countdown is done and {@link #onFinish()}
     *             is called.
     * @param countDownInterval The interval along the way to receive
     *             {@link #onTick(long)} callbacks.
     */
    public CountDownTimerUtils(Context context, TextView textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.mTextView = textView;
        this.context = context;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setClickable(false); //设置不可点击
        mTextView.setText(millisUntilFinished / 1000 + "秒"); //设置倒计时时间
        mTextView.setTextColor(context.getResources().getColor(R.color.theme)); //设置按钮为灰
        /*SpannableString spannableString = new SpannableString(mTextView.getText().toString());
        ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
        spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);*/
    }

    @Override
    public void onFinish() {
        mTextView.setText("重新获取");
        mTextView.setClickable(true);//重新获得点击
        mTextView.setTextColor(context.getResources().getColor(R.color.theme));
    }
}
